package com.future.hair.server.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.custom.Vo.OrderVo;
import com.future.hair.server.entity.*;
import com.future.hair.server.mapper.MallMapper;
import com.future.hair.server.mapper.UserInfoMapper;
import com.future.hair.server.mapper.UserMapper;
import com.future.hair.server.mapper.UserOrderMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("//hair/orderVo")
@Api(tags = "orderVo 订单")
public class OrderVoController {

    @Autowired
    private UserOrderMapper mapper;

    @Autowired
    private UserInfoMapper infoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MallMapper mallMapper;

    @ApiOperation(value = "查询 orderVo 列表")
    @RequestMapping(value = "/orderVoList", method = RequestMethod.POST)
    public CommonResult<List<OrderVo>> orderVoList(@RequestBody PageModel data, @RequestHeader String token) {
        List<OrderVo> orderVoList = new ArrayList<OrderVo>();
        List<UserOrder> orderList = orderList(data,token);
        for (UserOrder info:
                orderList) {
            orderVoList.add(getOrderVo(info));
        }
        return CommonResult.data(orderVoList);
    }

    @ApiOperation(value = "查询 orderVo 详情")
    @RequestMapping(value = "/orderVoDetail", method = RequestMethod.POST)
    public CommonResult<OrderVo> orderVoDetail(@RequestBody UserOrder data, @RequestHeader String token) {
        return CommonResult.data(getOrderVo(mapper.selectById(data.getId())));
    }

    @ApiOperation(value = "取消订单")
    @RequestMapping(value = "/orderCancel", method = RequestMethod.POST)
    public CommonResult<Integer> orderCancel(@RequestBody UserOrder data, @RequestHeader String token) {
        Integer id = data.getId();
        if (null==data.getId()){
            return CommonResult.failed("订单号不能为空");
        }
        QueryWrapper<UserOrder> orderWrapper = new QueryWrapper<UserOrder>();
        orderWrapper.eq("id", data.getId());
        orderWrapper.eq("user_id", JwtHelper.getByUserId(token));
        UserOrder order = new UserOrder();
        order.setType(-1);
        Integer result = mapper.update(order,orderWrapper);
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("更新失败");
        }
    }

    public List<UserOrder> orderList(PageModel data, String token) {
        QueryWrapper<UserOrder> orderWrapper = new QueryWrapper<UserOrder>();
        // 权限在4444以上不需要条件
        if (JwtHelper.getByOpenId(token)<=4444){
            // 权限在4444 查询店内订单
            if (JwtHelper.getByOpenId(token)==4444){
                QueryWrapper<UserInfo> infoWrapper = new QueryWrapper<UserInfo>();
                infoWrapper.eq("mall_id",JwtHelper.getByMallId(token));
                List<UserInfo> infoList = infoMapper.selectList(infoWrapper);
                List<Integer> ids = infoList.stream().map(UserInfo::getId).collect(Collectors.toList());
                orderWrapper.in("barber_id",ids);
            }else{
                // 判断是否查询没过期的订单
                if (null == data.getId()){
                    infoMapper.selectById(data.getId());
                    QueryWrapper<UserInfo> infoWrapper = new QueryWrapper<UserInfo>();
                    infoWrapper.eq("user_id",JwtHelper.getByUserId(token));
                    UserInfo info = infoMapper.selectOne(infoWrapper);
                    // 判断是否是店员
                    if (null==info){
                        System.out.print(JwtHelper.getByUserId(token));
                        orderWrapper.eq("user_id",JwtHelper.getByUserId(token));
                    }else{
                        orderWrapper.eq("barber_id",info.getId());
                    }
                }else {
                    if (null==infoMapper.selectById(data.getId())){
                        orderWrapper.eq("user_id",data.getId());
                    }else {
                        orderWrapper.ge("time",System.currentTimeMillis());
                        orderWrapper.eq("barber_id",data.getId());
                    }
                }

            }

        }
        return mapper.selectPage(new Page<>(data.getSize() , data.getCurrent()), orderWrapper).getRecords();
    }




    public OrderVo getOrderVo(UserOrder data){
        if (null==data){
            return null;
        }
        OrderVo orderVo = new OrderVo();

        orderVo.setUserOrder(data);

        UserInfo info = infoMapper.selectById(data.getBarberId());
        orderVo.setBarberId(info.getId());
        orderVo.setBarberName(info.getRealName());
        orderVo.setBarberAvatar(info.getAvatar());
        orderVo.setBarberMobile(info.getMobile());

        User user = userMapper.selectById(data.getUserId());
        orderVo.setUserId(user.getId());
        orderVo.setUserName(user.getUsername());
        orderVo.setUserAvatar(user.getAvatar());
        orderVo.setUserMobile(user.getMobile());

        Mall mall = mallMapper.selectById(info.getMallId());
        orderVo.setMallName(mall.getName());
        orderVo.setMallTel(mall.getTel());


        return orderVo;
    }

}
